package com.example.myapplication;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    private EditText etSelectedAddress;

    private final ActivityResultLauncher<Intent> addressLauncher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            result -> {
                if (result.getResultCode() == RESULT_OK && result.getData() != null) {
                    String selectedAddress = result.getData().getStringExtra("SELECTED_ADDRESS");
                    etSelectedAddress.setText(selectedAddress);
                }
            });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etSelectedAddress = findViewById(R.id.et_selected_address);
        Button btnChooseAddress = findViewById(R.id.btn_choose_address);

        btnChooseAddress.setOnClickListener(v -> {
            Intent intent = new Intent(MainActivity.this, AddressListActivity.class);
            addressLauncher.launch(intent);
        });
    }
}